//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeIncrement"/> class represents an increment operation.
    /// </summary>
    public class CodeIncrement : CodeValueReference
    {
        private CodeValueReference m_exp;

        /// <summary>
        /// Initializes a new instance of the <see cref="CodeIncrement"/>.
        /// </summary>
        /// <param name="exp">The value to increment.</param>
        public CodeIncrement(CodeValueReference exp)
        {
            m_exp = exp;
        }

        /// <summary>
        /// Generates the IL for the increment operation.  (x++)
        /// </summary>
        /// <param name="gen">The IL Generator to use.</param>
        public override void Generate(ILGenerator gen)
        {
            m_exp.GenerateSet(gen, new CodeAddOne(m_exp));
            m_exp.Generate(gen);
        }

        /// <summary>
        /// Generates the increment as a statement.  (x + 1)
        /// </summary>
        /// <param name="gen">The IL Generator to use.</param>
        public override void GenerateAsStatement(ILGenerator gen)
        {
            m_exp.GenerateSet(gen, new CodeAddOne(m_exp));
        }

        /// <summary>
        /// Generates an assignment to the value.
        /// </summary>
        /// <param name="gen">The IL Generator to use.</param>
        /// <param name="value">The value to assign.</param>
        public override void GenerateSet(ILGenerator gen, CodeExpression value)
        {
            m_exp.GenerateSet(gen, value);
        }

        /// <summary>
        /// Writes human readable code for the operation.
        /// </summary>
        /// <param name="cw">The code writer to use.</param>
        public override void PrintCode(CodeWriter cw)
        {
            m_exp.PrintCode(cw);
            cw.Write("++");
        }

        /// <summary>
        /// Gets the result type of the operation.
        /// </summary>
        public override Type ResultType
        {
            get { return m_exp.ResultType; }
        }
    }

    /// <summary>
    /// The <see cref="CodeAddOne"/> class represents an (x + 1) expression.
    /// </summary>
    public class CodeAddOne : CodeExpression
    {
        private CodeValueReference m_exp;
        private MethodInfo m_incMet;

        /// <summary>
        /// Initializes a new instance of an <see cref="CodeAddOne"/>
        /// </summary>
        /// <param name="exp">The value to add one to.</param>
        public CodeAddOne(CodeValueReference exp)
        {
            m_exp = exp;

            if (!exp.IsNumber)
            {
                m_incMet = exp.ResultType.GetMethod("op_Increment");
                if (m_incMet == null)
                {
                    throw new InvalidOperationException(
                        "Operator '++' cannot be applied to operand of type '" +
                        exp.ResultType.FullName + "'");
                }
            }
        }

        /// <summary>
        /// Generates the IL needed to add one to the value.
        /// </summary>
        /// <param name="gen">The IL generator to use.</param>
        public override void Generate(ILGenerator gen)
        {
            if (m_incMet != null)
            {
                CodeGenerationHelper.GenerateMethodCall(gen, null, m_incMet, m_exp);
                return;
            }

            m_exp.Generate(gen);
            Type t = m_exp.ResultType;
            switch (Type.GetTypeCode(t))
            {
                case TypeCode.Byte:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_U1);
                    break;

                case TypeCode.Double:
                    gen.Emit(OpCodes.Ldc_R8, 1);
                    gen.Emit(OpCodes.Add);
                    break;

                case TypeCode.Int16:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_I2);
                    break;

                case TypeCode.UInt32:
                case TypeCode.Int32:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    break;

                case TypeCode.UInt64:
                case TypeCode.Int64:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_U8);
                    break;

                case TypeCode.SByte:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_I1);
                    break;

                case TypeCode.Single:
                    gen.Emit(OpCodes.Ldc_R4, 1);
                    gen.Emit(OpCodes.Add);
                    break;

                case TypeCode.UInt16:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_U2);
                    break;
            }
        }

        /// <summary>
        /// Generates human readable code for the operation.
        /// </summary>
        /// <param name="cw">The code writer to use.</param>
        public override void PrintCode(CodeWriter cw)
        {
            m_exp.PrintCode(cw);
            cw.Write(" + 1");
        }

        /// <summary>
        /// The resulting type of the operation.
        /// </summary>
        public override Type ResultType
        {
            get { return m_exp.ResultType; }
        }
    }
}